
package org.japril.swing.jutil.xmloperations;

import org.japril.jutil.CharArrayPrintWriter;
import org.japril.jutil.xmloperations.XmlOperation;
import org.japril.jutil.xmloperations.XmlOperations;

/**
 * <p>
 *   {@link XmlOperationsReportCompiler} that creates plain text report.
 * </p>
 * @author Arne Halvorsen (AH)
 */
public final class PlainTextXmlOperationsReportCompiler 
  implements XmlOperationsReportCompiler 
{
  /**
   * <p>
   *   Constructor.
   * </p>
   */
  public PlainTextXmlOperationsReportCompiler(){}
  
  // Specified in XmlOperationsReportCompiler.
  public String getMimeType(){ return "text/plain"; }

  // Specified in XmlOperationsReportCompiler.
  public String getFileType(){ return "txt"; }

  // Specified in XmlOperationsReportCompiler.
  public String compile(XmlOperations ops) 
  {
    if (ops==null)
    {
      throw new NullPointerException("ops");
    }
    
    CharArrayPrintWriter capw=new CharArrayPrintWriter();
    
    int n=ops.size();
    for (int i=0; i<n; i++)
    {
      XmlOperation o=ops.getOperation(i);
      capw.println("Operation: "+o.getName());
      
      capw.println();
      capw.println("Paths:");
      int m=o.getPathCount();
      for (int j=0; j<m; j++)
      {
        String path=o.getPath(j);
        capw.println("  "+path);
      }
      
      String desc=o.getDesc();
      desc=(desc==null) ? "" : desc.trim();
      
      if (desc.length()>0)
      {
        capw.println();
        capw.println("Description:");
        capw.println();
        capw.println(desc);
      }
      capw.println();
      capw.println("---");
      capw.println();
    }
    
    return capw.toString();
  }
  
}
